﻿/*
 * BFind - A tool for finding football matches for trading on Betfair
 * Copyright 2009, 2010 Magnus Olsson <magnus72(at)gmail.com>
 * 
 * This file is part of BFind.
 * 
 * BFind is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * BFind is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with BFind.  If not, see <http://www.gnu.org/licenses/>
*/

using System;
using System.Collections.Generic;

namespace betfairfind.Rules.SilkBC
{
    class Filter2 : BaseRule
    {
        double MIN_HOME_ODDS;
        double MAX_HOME_ODDS;
        double MIN_AWAY_ODDS;
        double MAX_AWAY_ODDS;
        double MIN_CS_0_0_ODDS;
        double MAX_CS_0_0_ODDS;
        double MIN_CS_2_2_ODDS;
        double MAX_CS_2_2_ODDS;
        double MIN_MATCHODDS_MATCHED = 2000;

        public Filter2(Dictionary<string, string> config, List<MyMarket> inplaymarkets, Dictionary<long, MarketPricesCompressed> marketprices)
            : base()
        {
            MarketsUsedInside = new string[] { "match odds", "correct score" };

            try
            {
                MIN_HOME_ODDS = Convert.ToDouble(config["Rule.SilkBC.Filter2.MIN_HOME_ODDS"]);
                MAX_HOME_ODDS = Convert.ToDouble(config["Rule.SilkBC.Filter2.MAX_HOME_ODDS"]);
                MIN_AWAY_ODDS = Convert.ToDouble(config["Rule.SilkBC.Filter2.MIN_AWAY_ODDS"]);
                MAX_AWAY_ODDS = Convert.ToDouble(config["Rule.SilkBC.Filter2.MAX_AWAY_ODDS"]);
                MIN_CS_0_0_ODDS = Convert.ToDouble(config["Rule.SilkBC.Filter2.MIN_CS_0_0_ODDS"]);
                MAX_CS_0_0_ODDS = Convert.ToDouble(config["Rule.SilkBC.Filter2.MAX_CS_0_0_ODDS"]);
                MIN_CS_2_2_ODDS = Convert.ToDouble(config["Rule.SilkBC.Filter2.MIN_CS_2_2_ODDS"]);
                MAX_CS_2_2_ODDS = Convert.ToDouble(config["Rule.SilkBC.Filter2.MAX_CS_2_2_ODDS"]);
                MIN_MATCHODDS_MATCHED = Convert.ToDouble(config["Rule.SilkBC.Filter2.MIN_MATCHODDS_MATCHED"]);
            }
            catch (Exception e)
            {
                Logger.LogError(e.ToString(), true, true);
            }

            string criteria = @"" + MIN_HOME_ODDS + "/" +
            MAX_HOME_ODDS + "/" +
            MIN_AWAY_ODDS + "/" +
            MAX_AWAY_ODDS + "/" +
            MIN_CS_0_0_ODDS + "/" +
            MAX_CS_0_0_ODDS + "/" +
            MIN_CS_2_2_ODDS + "/" +
            MAX_CS_2_2_ODDS + "/" +
            MIN_MATCHODDS_MATCHED;

            Logger.Log("============================================================================", true, true);
            Logger.Log("Matches qualified for SilkBC Filter 2 (" + criteria + ")", true, true);
            Logger.Log("----------------------------------------------------------------------------", true, true);

            if (!CheckRequiredMarkets()) { return; }

            string[] games = FindSoccerGames.GetMatches();

            int selectionCount = 0;
            foreach (var game in games)
            {
                try
                {

                    MyMarket market_mo = FindSoccerGames.GetMarket(game, "match odds");
                    MarketPricesCompressed mpc_mo = marketprices[market_mo.marketId];

                    MyMarket market_cs = FindSoccerGames.GetMarket(game, "correct score");
                    MarketPricesCompressed mpc_cs = marketprices[market_cs.marketId];


                    double homeodds = mpc_mo.GetBestBackOdds(0);
                    double awayodds = mpc_mo.GetBestBackOdds(1);
                    double matched = mpc_mo.TotalMatched;
                    double cs_0_0odds = mpc_cs.GetBestBackOdds(0);
                    double cs_2_2odds = mpc_cs.GetBestBackOdds(10);


                    if ((homeodds >= MIN_HOME_ODDS && homeodds <= MAX_HOME_ODDS) &&
                        (awayodds>= MIN_AWAY_ODDS && homeodds <= MAX_AWAY_ODDS) &&
                       (cs_0_0odds >= MIN_CS_0_0_ODDS && cs_0_0odds <= MAX_CS_0_0_ODDS) &&
                       (cs_2_2odds >= MIN_CS_2_2_ODDS && cs_2_2odds <= MAX_CS_2_2_ODDS) &&
                        matched >= MIN_MATCHODDS_MATCHED)
                    {
                        Logger.Log(market_mo.marketTime + ", " + BF.convertPathToGame(game) + ", home: " + homeodds + ", away: " + awayodds + ", matched: " + config["CURRENCY_SIGN"] + ", 0-0: " + cs_0_0odds + ", 2-2: " + cs_2_2odds, true, true);
                        selectionCount++;
                    }
                }
                catch (Exception ex)
                {
                    Logger.LogError("Error in game " + game + Environment.NewLine + ex.ToString(), true, false);
                }

            }
            if (selectionCount == 0)
            {
                Logger.Log("No matches", true, true);
            }

        }
    }
}
